package com.ccf.business.knowledge.controller;

import cn.hutool.core.util.StrUtil;
import com.ccf.business.knowledge.model.vo.LabelRelationVO;
import com.ccf.business.knowledge.service.ILabelDataRelationService;
import com.ccf.business.knowledge.model.LabelDataRelation;
import com.common.model.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标注任务实体标记
 *
 * @author ccf
 * @date 2022-02-15 16:56:10
 */
@Slf4j
@RestController
@RequestMapping("/labeldatarelation")
@Api(tags = "标注记录-关系标记相关")
@RequiredArgsConstructor
public class LabelDataRelationController {

    private final ILabelDataRelationService labelDataRelationService;

//    /**
//     * 列表
//     */
//    @ApiOperation(value = "分页查询列表")
//    @GetMapping
//    public PageResult list(Query query) {
//        return labelDataRelationService.findList(query);
//    }


    /**
     * 列表
     */
    @ApiOperation(value = "查询关系列表", notes = "必须传递当前标注文件数据id")
    @ApiImplicitParam(value = "标注数据id", name = "dataId", required = true)
    @GetMapping("/relationList")
    public Result relationList(@RequestParam(name = "dataId", required = true) String dataId) {
        try {
            List<LabelRelationVO> resList = labelDataRelationService.relationList(dataId);
            return Result.succeed(resList, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

//    /**
//     * 查询
//     */
//    @ApiOperation(value = "查询")
//    @GetMapping("/{id}")
//    public Result getById(@PathVariable String id) {
//        LabelDataRelation model = labelDataRelationService.getById(id);
//        return Result.succeed(model, "查询成功");
//    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody LabelDataRelation labelDataRelation) {
        labelDataRelationService.saveOrUpdate(labelDataRelation);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        //批量删除
        if (StrUtil.contains(id, ",")){
            String[] ids = StrUtil.split(id, ",");
            labelDataRelationService.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        } else {
            labelDataRelationService.removeById(id);
        }
        return Result.succeed("删除成功");
    }
}
